package com.qingke.oms.factory;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.oms.command.Command;
import com.qingke.oms.command.user.ApplyNewGoodsCommand;
import com.qingke.oms.command.user.ApplyNewGoodsTypeCommand;
import com.qingke.oms.command.user.ApplyNewManufacturerCommand;
import com.qingke.oms.command.user.ConfirmReceiveCommand;
import com.qingke.oms.command.user.GoToShopCommand;
import com.qingke.oms.command.user.ListAllShopCommand;
import com.qingke.oms.command.user.ListMyShopCommand;
import com.qingke.oms.command.user.PayCommand;
import com.qingke.oms.command.user.RechargeCommand;
import com.qingke.oms.command.user.RegisterShopCommand;
import com.qingke.oms.command.user.UserHelpCommand;
import com.qingke.oms.pojo.User;

public class UserCommandFactory extends SystemCommandFactory{
	
	private static Map<CommandCode,Class<? extends Command>> userMap=new HashMap<>();
	
	
	static{
		userMap.put(CommandCode.NEWGOODS, ApplyNewGoodsCommand.class);
		userMap.put(CommandCode.NEWGOODSTYPE, ApplyNewGoodsTypeCommand.class);
		userMap.put(CommandCode.NEWMANUFACTURER, ApplyNewManufacturerCommand.class);
		userMap.put(CommandCode.RECEIVE, ConfirmReceiveCommand.class);
		userMap.put(CommandCode.GOTOSHOP, GoToShopCommand.class);
		userMap.put(CommandCode.PAY, PayCommand.class);
		userMap.put(CommandCode.RECHARGE, RechargeCommand.class);
		userMap.put(CommandCode.REGISTERSHOP, RegisterShopCommand.class);
		userMap.put(CommandCode.LISTALLSHOP, ListAllShopCommand.class);
		userMap.put(CommandCode.LISTMYSHOP, ListMyShopCommand.class);
		userMap.put(CommandCode.HELP, UserHelpCommand.class);
	}
	
	private User user;
	public UserCommandFactory(User user){
		this.user=user;
	}
	
	public User getUser() {
		return user;
	}

	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Class<? extends Command> commandClass=null;
		commandClass=userMap.get(cmdCode);
		if(commandClass==null)
			return super.buildCommand(cmdCode);
		
		Command command=null;
		try {
			command=commandClass.getConstructor(User.class).newInstance(this.getUser());
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return command;
	}
}
